import java.util.Arrays;

class Solution {
    public static void duplicateZeros(int[] arr) {
        int cur = 0,dest = -1;
        while(dest < arr.length) {
            if(arr[cur] == 0) {
                dest += 2;
                if(dest >= arr.length-1) {
                    break;
                }
            }else {
                dest++;
                if(dest >= arr.length-1) {
                    break;
                }
            }
            cur++;
        }
        if(dest >= arr.length) {
            arr[dest-1] = 0;
            dest-=2;
            cur--;
            while(dest >= 0) {
                if(arr[cur] == 0 && dest >= 1) {
                    arr[dest] = 0;
                    arr[dest-1] = 0;
                    dest -= 2;
                    if(dest < 0) {
                        break;
                    }
                }else {
                    arr[dest] = arr[cur];
                    dest--;
                    if(dest < 0) {
                        break;
                    }
                }
                cur--;
            }
        }else {
            while(dest >= 0) {
                if(arr[cur] == 0) {
                    arr[dest] = 0;
                    arr[dest-1] = 0;
                    dest -= 2;
                    if(dest < 0) {
                        break;
                    }
                }else {
                    arr[dest] = arr[cur];
                    dest--;
                    if(dest< 0) {
                        break;
                    }
                }
                cur--;
            }
        }
    }

    public static void main(String[] args) {
        int[] arr = {1,0,2,3,0,4,5,0};
        duplicateZeros(arr);
        System.out.println(Arrays.toString(arr));
    }
}